<!DOCTYPE html>
<html>

<head lang="en">
    <title>点评</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/travel.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css">
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/common.js"></script>
    <style>
        .rect{
            border: #717981 solid 1px;
            color: #717981;
            font-size: 12px;
            text-align: center;
            margin: 5px 5px 5px 5px;
        }

        .rect img {
            width: auto;
            height: 80px;
            max-width: 100%;
            max-height: 100%;
        }
        img {
            vertical-align: middle;
            border-style: none;
        }

    </style>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:window.history.go(-1)">
                <span><i class="fa fa-chevron-left" ></i></span>
            </a>

        </div>
        <div class="col">
            <span>点评</span>
        </div>
        <div class="col"></div>
    </div>
</div>

<div class="comment">
    <div class="container star">
        <p>你对该旅游地的评价</p>
        <i class="fa fa-star-o fa-lg" data-num="1"></i>
        <i class="fa fa-star-o fa-lg" data-num="2"></i>
        <i class="fa fa-star-o fa-lg" data-num="3"></i>
        <i class="fa fa-star-o fa-lg" data-num="4"></i>
        <i class="fa fa-star-o fa-lg" data-num="5"></i>
    </div>
    <hr />
    <div class="container tag">
        <p>请为该旅游地选择合适的标签（大家都在评）</p>
        <div class="btn-group-toggle" data-toggle="buttons" render-loop="list">
            <label class="btn btn-outline-danger ">
                <input type="checkbox" autocomplete="off" render-value="list.name"><span render-html="list.name"></span>
            </label>
        </div>

        <p>添加你自己的标签</p>
        <input name="myTags" class="form-control"  placeholder="请使用空格隔开标签，每个标签不超8个字"   />

        <p>更多评价内容</p>
        <textarea name="content" class="form-control" rows="5" placeholder="请填写你对该旅游地的评价"></textarea>
    </div>
    <div class="container imgs ">
        <p>上传图片</p>

        <div class="row">
            <form id="imgForm" action="/images" method="post" enctype="multipart/form-data">
                <input type="file" style="display: none;" name="multipartFile"  id="multipartFile"/>
            </form>
            <div class="col-3 rect">
                <span>上传</span>
            </div>
            <div class="col-3 rect">
                <span>上传</span>
            </div>
            <div class="col-3 rect">
                <span>上传</span>
            </div>


        </div>
    </div>

</div>



<div class="operation">
    <button class="btn" onclick="window.history.go(-1)">放弃评价</button>
    <button class="btn" id="submitBtn">提交评价</button>
</div>

<script>
    $(function () {
        var params = getParams();
        if(params.id){
            //图片所在的div
            var rect;
            //判断是否已经有图片------------------------------------
            $(".rect").click(function () {
                rect = $(this);
                if(rect.html().indexOf("img")>-1){
                    //是否删除
                    $(document).dialog({
                        type : 'confirm',
                        closeBtnShow: true,
                        content: '是否需要删除',
                        onClickConfirmBtn: function(){
                            rect.html('<span>上传</span>');
                        },
                        buttonClassCancel: 'custom-cancel',
                    });
                }else{
                    //打开选择图片窗口
                    $('[name=multipartFile]').click();
                    //js选择图片后的回显

                }
            })

            //上传图片-------------------------------------------
            $('#multipartFile').change(function () {
                var img = this.value;
                console.log(img);
                if(img){
                    $("#imgForm").ajaxSubmit(function (data) {
                        if (data.status == 1) {
                            rect.html("<img width='100%' src='"+data.url+"'/>")
                        } else {
                            $(document).dialog({
                                content: data.msg
                            });
                        }
                    })

                }
            });

            //旅游地的评价----------------------------------------------
            $(".star i").click(function () {
                var starNum = $(this).data("num");
                //console.log(starNum);

                var html='<p>你对该旅游地的评价</p>'
                for(i=1;i<=starNum;i++){
                    html+= '<i class="fa fa-star fa-lg" data-num="'+i+'"></i> '
                }
                for(i=starNum+1; i<=5;i++){
                    html+= '<i class="fa fa-star-o fa-lg" data-num="'+i+'"></i> '
                }
                $(".star").html(html)
            })

            //查询出所有的标签-----------------------------------------------
            $.get("/strategies/"+params.id+"/tags",{pageSize:0},function (data) {
                console.log(data.list);
                $(".tag").renderValues({list:data.list})
            })

            //提交表单---------------------------------------------------------
            $("#submitBtn").click(function () {
                //首先确定传入的参数
                //1.星星数
                var starNum =$(".fa-star").length
                //2.图片数
                var imgs = $(".imgs img");
                var imgUrls = "";
                for(i=0;i<imgs.length;i++){
                    imgUrls+=$(imgs[i]).attr("src")+";";
                }




                //系统推荐标签
                //标签数组
                var tagNames = [];
                //获取选中的标签(系统推荐标签)
                var tagInputs = $("label.active").find("input");
                //标签数组的索引
                var i ;
                for (i = 0; i < tagInputs.length;i++) {
                    tagNames[i] = $(tagInputs[i]).val();
                }

                if($("[name=myTags]").val().trim()!=""){
                    tagNames[i++] = $("[name=myTags]").val().trim();
                }
                console.log(tagNames);


                console.log(tagNames);
                //传递参数时不需要添加[]
                jQuery.ajaxSettings.traditional = true;

                var comment = {
                    'starNum':starNum,
                    'imgUrls':imgUrls,
                    'strategy.id':params.id,
                    'content':$("[name=content]").val(),
                    'tagNames':tagNames
                }
                console.log(comment);


                //发送post请求新增数据
                $.post("/strategies/comments",comment,function (data) {
                    console.log(comment);
                    if(data.success){
                        location.href="/strategyCatalogs.html?id="+params.id;
                    }else{
                        $(document).dialog({
                            autoClose: 2500,
                            content: data.msg
                        });
                    }
                })



            })


        }
    })

</script>



</body>


</html>